
enum MOTOR_NUM {
    //% block="1"
    1,
    //% block="2"
    2,
    //% block="3"
    3,
    //% block="4"
    4,
    //% block="5"
    5,
    //% block="6"
    6
}


enum FPV_DATACH {
    //% block="右摇杆X轴"
    Joystick_RX,
    //% block="右摇杆Y轴"
    Joystick_RY,
    //% block="左摇杆X轴"
    Joystick_LX,
    //% block="左摇杆Y轴"
    Joystick_LY,
    //% block="一键起飞"
    Takeoff,
    //% block="一键降落"
    Land,
    //% block="紧急停止"
    Stop,
    //% block="无头模式"
    Nodir,
    //% block="陀螺仪校正"
    Rectify
}

//% color="#5ba55b" iconWidth=50 iconHeight=40
namespace esp32iotrobot {

    //% block="FPV图传摄像头模块 初始化硬串口[UARTX] RX管脚#[RXPIN]" blockType="command"
    //% UARTX.shadow="dropdown" UARTX.options="UARTX_HardwareSerial" 
    //% RXPIN.shadow="dropdown" RXPIN.options="PIN_DigitalRead" 
    export function FPV_Init(parameter: any, block: any) {
        let rxpin = parameter.RXPIN.code;
        let uartx = parameter.UARTX.code;
        Generator.addInclude('FPV', '#include "RL_FPV_MP.h"');
        Generator.addObject(`fpvSerial`, `FPV `, `fpvSerial;//UART1`);
        Generator.addSetup(`fpvSerial.begin`, `fpvSerial.begin(&${uartx},${rxpin},-1);//定义默认接收管脚 23号`);
        //Generator.addCode(`fpvSerial.FpvReadAll();`);
    }
    //% block="FPV图传摄像头模块 读取FPV控制数据" blockType="reporter"
    export function FPV_ReadAll(parameter: any, block: any) {
       
        Generator.addCode(`fpvSerial.FpvReadAll()`);
    }

    //% block="FPV图传摄像头模块 返回FPV[CH]的数据" blockType="reporter"
    //% CH.shadow="dropdown" CH.options="FPV_DATACH"
    export function FPV_ReadByte(parameter: any, block: any) {
        let ch = parameter.CH.code;

        Generator.addCode(`fpvSerial.FpvReadByte(${ch})`);
    }


    //% block="ESP32_双PWM电机控制 组号[NUM] #A管脚#[APIN] #B管脚#[BPIN]" blockType="command"
    //% NUM.shadow="dropdown" NUM.options="MOTOR_NUM" 
    //% APIN.shadow="dropdown" APIN.options="PIN_DigitalWrite"
    //% BPIN.shadow="dropdown" BPIN.options="PIN_DigitalWrite"  
    export function Esp32_Motor_Init(parameter: any, block: any) {
        let num = parameter.NUM.code;
        let apin = parameter.APIN.code;
        let bpin = parameter.BPIN.code;
        Generator.addInclude('ESP32_Motor', '#include <RL_ESP32_Motor.h>');
        Generator.addObject(`Esp32_Motor_${num}`, `Motor `, `Esp32_Motor_${num}(${num},${apin},${bpin});`);
        Generator.addSetup(`Esp32_Motor_${num}mcpwm_begin`, `Esp32_Motor_${num}.mcpwm_begin();`);
    }

    //% block="ESP32_双PWM电机控制 组号[NUM] 速度(-100~100)[SPEED]" blockType="command"
    //% NUM.shadow="dropdown" NUM.options="MOTOR_NUM" 
    //% SPEED.shadow="range" SPEED.params.min=-100 SPEED.params.max=100 SPEED.defl=0
    export function Esp32_Motor_Speed(parameter: any, block: any) {
        let num = parameter.NUM.code;
        let speed = parameter.SPEED.code

        Generator.addCode(`Esp32_Motor_${num}.Motor_Speed(${speed});`);
    }
    /*//% block="获取ESP32旋转编码器 管脚#[APIN] 的数值" blockType="reporter"
    //% APIN.shadow="dropdown" APIN.options="PIN_DigitalRead" 
    export function ESP32_Encoder_getCountA(parameter: any, block: any) {
        let apin = parameter.APIN.code;
        Generator.addInclude('DFRobot_Servo', '#include <ESP32Encoder.h>');
        Generator.addObject(`ESP32Encoder_encoder_${apin}`, `ESP32Encoder `, `encoder_${apin};`);
        Generator.addSetup(`encoder_${apin}_attachSingleEdge`, `encoder_${apin}.attachSingleEdge(${apin},-1);`);
        Generator.addCode(`encoder_${apin}.getCount()`);
    }*/
    //% block="设置舵机 管脚#[PIN] 角度为[ANGLE]" blockType="command"
    //% PIN.shadow="dropdown" PIN.options="PIN_DigitalWrite" 
    //% ANGLE.shadow="angle" ANGLE.params.edge=1  ANGLE.defl=90
    export function Esp32_Servo(parameter: any, block: any) {
        let pin = parameter.PIN.code;
        let angle = parameter.ANGLE.code

        Generator.addInclude('DFRobot_Servo', '#include <DFRobot_Servo.h>');
        Generator.addObject(`Servo_${pin}`, `Servo `, `servo_${pin};`);
        Generator.addSetup(`servo_${pin}_attach`, `servo_${pin}.attach(${pin});`);
        Generator.addCode(`servo_${pin}.angle(abs(${angle}));`);
    }

}
